package com.unclear.codegeneration.controller;

import com.unclear.codegeneration.vo.BaseDTO;
import com.unclear.codegeneration.vo.EntityDTO;
import com.unclear.codegeneration.vo.MethodDTO;
import com.unclear.codegeneration.vo.groupDTO;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <pre>类名: CodeController</pre>
 * <pre>描述: </pre>
 * <pre>版权: </pre>
 * <pre>日期: 2023年11月11日 12:08 </pre>
 * <pre>作者: unclearLi</pre>
 */

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    FreeMarkerConfigurer configuration;

    @PostMapping("/bulid")
    private void buildCode(@RequestBody groupDTO groupDTO) throws TemplateException, IOException {
        /*Map<String, Object> map1 = new HashMap<>();
        BaseDTO baseDTO = BaseDTO.builder().viewName("Ssgx").baseUrl("ssgx").serviceName("Ssgx").mapperName("Ssgx").packagePath("cn.com.servyou.apaas.gxqgsj").desc("税视高新").build();
        map1.put("baseDTO", baseDTO);

        MethodDTO qypm = MethodDTO.builder().name("Qypm").queryParam("QueryParamVO").returnType("List").desc("测试").build();
        MethodDTO tsQySs = MethodDTO.builder().name("TsQySs").queryParam("QueryParamVO").returnType("List").desc("测试").build();
        MethodDTO fssAndSzss = MethodDTO.builder().name("FssAndSzss").queryParam("QueryParamVO").returnType("List").desc("测试").build();
        MethodDTO fhyAndcyss = MethodDTO.builder().name("FhyAndcyss").queryParam("QueryParamVO").returnType("List").desc("测试").build();
        List<MethodDTO> methodDTOS = ImmutableList.of(qypm, tsQySs, fssAndSzss, fhyAndcyss);
        map1.put("methods", methodDTOS);

        FieldDTO name = FieldDTO.builder().name("name").type("String").desc("sss").build();
        FieldDTO jrkse = FieldDTO.builder().name("jrkse").type("double").desc("sss").build();
        FieldDTO snjrkse = FieldDTO.builder().name("snjrkse").type("double").desc("sss").build();
        FieldDTO tb = FieldDTO.builder().name("tb").type("double").desc("sss").build();

        EntityDTO entityDTO = EntityDTO.builder().name("Qypm").fieldList(ImmutableList.of(name, jrkse, snjrkse, tb)).packagePath("cn.com.servyou.apaas.gxqgsj.api.vis.ssgx").desc("企业排名").build();
        List<EntityDTO> entitys = ImmutableList.of(entityDTO);
        map1.put("entitys", entitys);*/

        BaseDTO baseDTO = groupDTO.getBaseDTO();
        List<EntityDTO> entitys = groupDTO.getEntitys();
        List<MethodDTO> methods = groupDTO.getMethods();

        File viewFile = new File("src/main/resources/code/" + baseDTO.getViewName() + "Controller.java");
        Writer viewWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(viewFile), StandardCharsets.UTF_8));
        Template viewTtemplate = configuration.getConfiguration().getTemplate("controller.ftlh");
        viewTtemplate.process(groupDTO, viewWriter);
        viewWriter.close();

        File iServieFile = new File("src/main/resources/code/I" + baseDTO.getServiceName() + "Service.java");
        Writer iServieWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(iServieFile), StandardCharsets.UTF_8));
        Template iServieTemplate = configuration.getConfiguration().getTemplate("Iservice.ftlh");
        iServieTemplate.process(groupDTO, iServieWriter);
        iServieWriter.close();

        File serviceFile = new File("src/main/resources/code/" + baseDTO.getServiceName() + "Service.java");
        Writer serviceWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(serviceFile), StandardCharsets.UTF_8));
        Template serviceTemplate = configuration.getConfiguration().getTemplate("service.ftlh");
        serviceTemplate.process(groupDTO, serviceWriter);
        serviceWriter.close();

        File mapperFile = new File("src/main/resources/code/" + baseDTO.getMapperName() + "Mapper.java");
        Writer mapperWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(mapperFile), StandardCharsets.UTF_8));
        Template mapperTemplate = configuration.getConfiguration().getTemplate("mapper.ftlh");
        mapperTemplate.process(groupDTO, mapperWriter);
        mapperWriter.close();


        File mybatisFile = new File("src/main/resources/code/" + baseDTO.getMapperName() + "Mapper.xml");
        Writer mybatisWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(mybatisFile), StandardCharsets.UTF_8));
        Template mybatisTemplate = configuration.getConfiguration().getTemplate("mybatis.ftlh");
        mybatisTemplate.process(groupDTO, mybatisWriter);
        mybatisWriter.close();

        for (EntityDTO entityDto : entitys) {
            File voFile = new File("src/main/resources/code/" + entityDto.getName() + "DTO.java");
            Writer voWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(voFile), StandardCharsets.UTF_8));
            Template voTemplate = configuration.getConfiguration().getTemplate("vo.ftlh");
            voTemplate.process(entityDto, voWriter);
            voWriter.close();
        }

    }


}
